package com.publiccms.views.directive.tools;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.publiccms.common.base.AbstractTemplateDirective;
import com.publiccms.logic.component.MetadataComponent;
import com.sanluan.common.handler.RenderHandler;

@Component
public class MetadataDirective extends AbstractTemplateDirective {

    @Override
    public void execute(RenderHandler handler) throws IOException, Exception {
        String path = handler.getString("path");
        String dir = handler.getString("dir");
        String type = handler.getString("type");
        if (notEmpty(path) && !path.endsWith("/")) {
            handler.put("path", path)
                    .put("type", type)
                    .put("object",
                            metadataComponent.getTemplateMetadata(siteComponent.getTemplateFilePath(getSiteId(handler), type,
                                    path))).render();
        } else if (null != dir) {
            handler.put("object",
                    metadataComponent.getMetadataMap(siteComponent.getTemplateFilePath(getSiteId(handler), type, dir))).render();
        }
    }

    @Autowired
    private MetadataComponent metadataComponent;
}
